home *** CD-ROM | disk | FTP | other *** search
/ Software of the Month Club 2000 October / Software of the Month - Ultimate Collection Shareware 277.iso / pc / PROGRAMS / UTILITY / WINLINUX / DATA1.CAB / usr_-_Usr_Files / BIN / FIND2PER.{BH < prev    next >
Text File  |  1999-09-17  |  13KB  |  589 lines

  1. #!/usr/bin/perl
  2.     eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
  3.       if $running_under_some_shell;
  4. $startperl = "#!/usr/bin/perl";
  5. $perlpath = "/usr/bin/perl";
  6.  
  7. # Modified September 26, 1993 to provide proper handling of years after 1999
  8. #   Tom Link <tml+@pitt.edu>
  9. #   University of Pittsburgh
  10. # Modified April 7, 1998 with nasty hacks to implement the troublesome -follow
  11. #  Billy Constantine <wdconsta@cs.adelaide.edu.au> <billy@smug.adelaide.edu.au>
  12. #  University of Adelaide, Adelaide, South Australia
  13.  
  14. while ($ARGV[0] =~ /^[^-!(]/) {
  15.     push(@roots, shift);
  16. }
  17. @roots = ('.') unless @roots;
  18. for (@roots) { $_ = "e($_); }
  19. $roots = join(',', @roots);
  20.  
  21. $indent = 1;
  22. $stat = 'lstat';
  23. $decl = '';
  24.  
  25. while (@ARGV) {
  26.     $_ = shift;
  27.     s/^-// || /^[()!]/ || die "Unrecognized switch: $_\n";
  28.     if ($_ eq '(') {
  29.     $out .= &tab . "(\n";
  30.     $indent++;
  31.     next;
  32.     }
  33.     elsif ($_ eq ')') {
  34.     $indent--;
  35.     $out .= &tab . ")";
  36.     }
  37.     elsif ($_ eq 'follow') {
  38.     $stat = 'stat';
  39.     $decl = '%already_seen = ();';
  40.     $out .= &tab . '(not $already_seen{"$dev,$ino"}) &&';
  41.     $out .= "\n" . &tab . '(($already_seen{"$dev,$ino"} = !(-d _)) || 1)';
  42.     }
  43.     elsif ($_ eq '!') {
  44.     $out .= &tab . "!";
  45.     next;
  46.     }
  47.     elsif ($_ eq 'name') {
  48.     $out .= &tab;
  49.     $pat = &fileglob_to_re(shift);
  50.     $out .= '/' . $pat . "/";
  51.     }
  52.     elsif ($_ eq 'perm') {
  53.     $onum = shift;
  54.     die "Malformed -perm argument: $onum\n" unless $onum =~ /^-?[0-7]+$/;
  55.     if ($onum =~ s/^-//) {
  56.         $onum = '0' . sprintf("%o", oct($onum) & 017777);    # s/b 07777 ?
  57.         $out .= &tab . "((\$mode & $onum) == $onum)";
  58.     }
  59.     else {
  60.         $onum = '0' . $onum unless $onum =~ /^0/;
  61.         $out .= &tab . "((\$mode & 0777) == $onum)";
  62.     }
  63.     }
  64.     elsif ($_ eq 'type') {
  65.     ($filetest = shift) =~ tr/s/S/;
  66.     $out .= &tab . "-$filetest _";
  67.     }
  68.     elsif ($_ eq 'print') {
  69.     $out .= &tab . 'print("$name\n")';
  70.     }
  71.     elsif ($_ eq 'print0') {
  72.     $out .= &tab . 'print("$name\0")';
  73.     }
  74.     elsif ($_ eq 'fstype') {
  75.     $out .= &tab;
  76.     $type = shift;
  77.     if ($type eq 'nfs')
  78.         { $out .= '($dev < 0)'; }
  79.     else
  80.         { $out .= '($dev >= 0)'; }
  81.     }
  82.     elsif ($_ eq 'user') {
  83.     $uname = shift;
  84.     $out .= &tab . "(\$uid == \$uid{'$uname'})";
  85.     $inituser++;
  86.     }
  87.     elsif ($_ eq 'group') {
  88.     $gname = shift;
  89.     $out .= &tab . "(\$gid == \$gid{'$gname'})";
  90.     $initgroup++;
  91.     }
  92.     elsif ($_ eq 'nouser') {
  93.     $out .= &tab . '!defined $uid{$uid}';
  94.     $inituser++;
  95.     }
  96.     elsif ($_ eq 'nogroup') {
  97.     $out .= &tab . '!defined $gid{$gid}';
  98.     $initgroup++;
  99.     }
  100.     elsif ($_ eq 'links') {
  101.     $out .= &tab . '($nlink ' . &n(shift);
  102.     }
  103.     elsif ($_ eq 'inum') {
  104.     $out .= &tab . '($ino ' . &n(shift);
  105.     }
  106.     elsif ($_ eq 'size') {
  107.     $out .= &tab . '(int(((-s _) + 511) / 512) ' . &n(shift);
  108.     }
  109.     elsif ($_ eq 'atime') {
  110.     $out .= &tab . '(int(-A _) ' . &n(shift);
  111.     }
  112.     elsif ($_ eq 'mtime') {
  113.     $out .= &tab . '(int(-M _) ' . &n(shift);
  114.     }
  115.     elsif ($_ eq 'ctime') {
  116.     $out .= &tab . '(int(-C _) ' . &n(shift);
  117.     }
  118.     elsif ($_ eq 'exec') {
  119.     for (@cmd = (); @ARGV && $ARGV[0] ne ';'; push(@cmd,shift)) { }
  120.     shift;
  121.     $_ = "@cmd";
  122.     if (m#^(/bin/)?rm -f {}$#) {
  123.         if (!@ARGV) {
  124.         $out .= &tab . 'unlink($_)';
  125.         }
  126.         else {
  127.         $out .= &tab . '(unlink($_) || 1)';
  128.         }
  129.     }
  130.     elsif (m#^(/bin/)?rm {}$#) {
  131.         $out .= &tab . '(unlink($_) || warn "$name: $!\n")';
  132.     }
  133.     else {
  134.         for (@cmd) { s/'/\\'/g; }
  135.         $" = "','";
  136.         $out .= &tab . "&exec(0, '@cmd')";
  137.         $" = ' ';
  138.         $initexec++;
  139.     }
  140.     }
  141.     elsif ($_ eq 'ok') {
  142.     for (@cmd = (); @ARGV && $ARGV[0] ne ';'; push(@cmd,shift)) { }
  143.     shift;
  144.     for (@cmd) { s/'/\\'/g; }
  145.     $" = "','";
  146.     $out .= &tab . "&exec(1, '@cmd')";
  147.     $" = ' ';
  148.     $initexec++;
  149.     }
  150.     elsif ($_ eq 'prune') {
  151.     $out .= &tab . '($prune = 1)';
  152.     }
  153.     elsif ($_ eq 'xdev') {
  154.     $out .= &tab . '!($prune |= ($dev != $topdev))';
  155.     }
  156.     elsif ($_ eq 'newer') {
  157.     $out .= &tab;
  158.     $file = shift;
  159.     $newername = 'AGE_OF' . $file;
  160.     $newername =~ s/[^\w]/_/g;
  161.     $newername = "\$$newername";
  162.     $out .= "(-M _ < $newername)";
  163.     $initnewer .= "$newername = -M " . "e($file) . ";\n";
  164.     }
  165.     elsif ($_ eq 'eval') {
  166.     $prog = "e(shift);
  167.     $out .= &tab . "eval $prog";
  168.     }
  169.     elsif ($_ eq 'depth') {
  170.     $depth++;
  171.     next;
  172.     }
  173.     elsif ($_ eq 'ls') {
  174.     $out .= &tab . "&ls";
  175.     $initls++;
  176.     }
  177.     elsif ($_ eq 'tar') {
  178.     $out .= &tab;
  179.     die "-tar must have a filename argument\n" unless @ARGV;
  180.     $file = shift;
  181.     $fh = 'FH' . $file;
  182.     $fh =~ s/[^\w]/_/g;
  183.     $out .= "&tar($fh)";
  184.     $file = '>' . $file;
  185.     $initfile .= "open($fh, " . "e($file) .
  186.       qq{) || die "Can't open $fh: \$!\\n";\n};
  187.     $inittar++;
  188.     $flushall = "\n&tflushall;\n";
  189.     }
  190.     elsif (/^n?cpio$/) {
  191.     $depth++;
  192.     $out .= &tab;
  193.     die "-$_ must have a filename argument\n" unless @ARGV;
  194.     $file = shift;
  195.     $fh = 'FH' . $file;
  196.     $fh =~ s/[^\w]/_/g;
  197.     $out .= "&cpio('" . substr($_,0,1) . "', $fh)";
  198.     $file = '>' . $file;
  199.     $initfile .= "open($fh, " . "e($file) .
  200.       qq{) || die "Can't open $fh: \$!\\n";\n};
  201.     $initcpio++;
  202.     $flushall = "\n&flushall;\n";
  203.     }
  204.     else {
  205.     die "Unrecognized switch: -$_\n";
  206.     }
  207.     if (@ARGV) {
  208.     if ($ARGV[0] eq '-o') {
  209.         { local($statdone) = 1; $out .= "\n" . &tab . "||\n"; }
  210.         $statdone = 0 if $indent == 1 && $delayedstat;
  211.         $saw_or++;
  212.         shift;
  213.     }
  214.     else {
  215.         $out .= " &&" unless $ARGV[0] eq ')';
  216.         $out .= "\n";
  217.         shift if $ARGV[0] eq '-a';
  218.     }
  219.     }
  220. }
  221.  
  222. print <<"END";
  223. $startperl
  224.     eval 'exec $perlpath -S \$0 \${1+"\$@"}'
  225.     if \$running_under_some_shell;
  226.  
  227. END
  228.  
  229. if ($initls) {
  230.     print <<'END';
  231. @rwx = ('---','--x','-w-','-wx','r--','r-x','rw-','rwx');
  232. @moname = (Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec);
  233.  
  234. END
  235. }
  236.  
  237. if ($inituser || $initls) {
  238.     print 'while (($name, $pw, $uid) = getpwent) {', "\n";
  239.     print '    $uid{$name} = $uid{$uid} = $uid;', "\n" if $inituser;
  240.     print '    $user{$uid} = $name unless $user{$uid};', "\n" if $initls;
  241.     print "}\n\n";
  242. }
  243.  
  244. if ($initgroup || $initls) {
  245.     print 'while (($name, $pw, $gid) = getgrent) {', "\n";
  246.     print '    $gid{$name} = $gid{$gid} = $gid;', "\n" if $initgroup;
  247.     print '    $group{$gid} = $name unless $group{$gid};', "\n" if $initls;
  248.     print "}\n\n";
  249. }
  250.  
  251. print $initnewer, "\n" if $initnewer;
  252.  
  253. print $initfile, "\n" if $initfile;
  254.  
  255. $find = $depth ? "finddepth" : "find";
  256. print <<"END";
  257. require "$find.pl";
  258.  
  259. # Traverse desired filesystems
  260.  
  261. $decl
  262. &$find($roots);
  263. $flushall
  264. exit;
  265. sub wanted {
  266. $out;
  267. }
  268.  
  269. END
  270.  
  271. if ($initexec) {
  272.     print <<'END';
  273. sub exec {
  274.     local($ok, @cmd) = @_;
  275.     foreach $word (@cmd) {
  276.     $word =~ s#{}#$name#g;
  277.     }
  278.     if ($ok) {
  279.     local($old) = select(STDOUT);
  280.     $| = 1;
  281.     print "@cmd";
  282.     select($old);
  283.     return 0 unless <STDIN> =~ /^y/;
  284.     }
  285.     chdir $cwd;        # sigh
  286.     system @cmd;
  287.     chdir $dir;
  288.     return !$?;
  289. }
  290.  
  291. END
  292. }
  293.  
  294. if ($initls) {
  295.     print <<"INTERP", <<'END';
  296. sub ls {
  297.     (\$dev,\$ino,\$mode,\$nlink,\$uid,\$gid,\$rdev,\$sizemm,
  298.       \$atime,\$mtime,\$ctime,\$blksize,\$blocks) = $stat\(_\);
  299. INTERP
  300.  
  301.     $pname = $name;
  302.  
  303.     if (defined $blocks) {
  304.     $blocks = int(($blocks + 1) / 2);
  305.     }
  306.     else {
  307.     $blocks = int(($size + 1023) / 1024);
  308.     }
  309.  
  310.     if    (-f _) { $perms = '-'; }
  311.     elsif (-d _) { $perms = 'd'; }
  312.     elsif (-c _) { $perms = 'c'; $sizemm = &sizemm; }
  313.     elsif (-b _) { $perms = 'b'; $sizemm = &sizemm; }
  314.     elsif (-p _) { $perms = 'p'; }
  315.     elsif (-S _) { $perms = 's'; }
  316.     else         { $perms = 'l'; $pname .= ' -> ' . readlink($_); }
  317.  
  318.     $tmpmode = $mode;
  319.     $tmp = $rwx[$tmpmode & 7];
  320.     $tmpmode >>= 3;
  321.     $tmp = $rwx[$tmpmode & 7] . $tmp;
  322.     $tmpmode >>= 3;
  323.     $tmp = $rwx[$tmpmode & 7] . $tmp;
  324.     substr($tmp,2,1) =~ tr/-x/Ss/ if -u _;
  325.     substr($tmp,5,1) =~ tr/-x/Ss/ if -g _;
  326.     substr($tmp,8,1) =~ tr/-x/Tt/ if -k _;
  327.     $perms .= $tmp;
  328.  
  329.     $user = $user{$uid} || $uid;
  330.     $group = $group{$gid} || $gid;
  331.  
  332.     ($sec,$min,$hour,$mday,$mon,$year) = localtime($mtime);
  333.     $moname = $moname[$mon];
  334.     if (-M _ > 365.25 / 2) {
  335.     $timeyear = $year + 1900;
  336.     }
  337.     else {
  338.     $timeyear = sprintf("%02d:%02d", $hour, $min);
  339.     }
  340.  
  341.     printf "%5lu %4ld %-10s %2d %-8s %-8s %8s %s %2d %5s %s\n",
  342.         $ino,
  343.          $blocks,
  344.               $perms,
  345.                 $nlink,
  346.                 $user,
  347.                      $group,
  348.                       $sizemm,
  349.                           $moname,
  350.                          $mday,
  351.                              $timeyear,
  352.                              $pname;
  353.     1;
  354. }
  355.  
  356. sub sizemm {
  357.     sprintf("%3d, %3d", ($rdev >> 8) & 255, $rdev & 255);
  358. }
  359.  
  360. END
  361. }
  362.  
  363. if ($initcpio) {
  364. print <<'START', <<"INTERP", <<'END';
  365. sub cpio {
  366.     local($nc,$fh) = @_;
  367.     local($text);
  368.  
  369.     if ($name eq 'TRAILER!!!') {
  370.     $text = '';
  371.     $size = 0;
  372.     }
  373.     else {
  374. START
  375.     (\$dev,\$ino,\$mode,\$nlink,\$uid,\$gid,\$rdev,\$size,
  376.       \$atime,\$mtime,\$ctime,\$blksize,\$blocks) = $stat\(_\);
  377. INTERP
  378.     if (-f _) {
  379.         open(IN, "./$_\0") || do {
  380.         warn "Couldn't open $name: $!\n";
  381.         return;
  382.         };
  383.     }
  384.     else {
  385.         $text = readlink($_);
  386.         $size = 0 unless defined $text;
  387.     }
  388.     }
  389.  
  390.     ($nm = $name) =~ s#^\./##;
  391.     $nc{$fh} = $nc;
  392.     if ($nc eq 'n') {
  393.     $cpout{$fh} .=
  394.       sprintf("%06o%06o%06o%06o%06o%06o%06o%06o%011lo%06o%011lo%s\0",
  395.         070707,
  396.         $dev & 0777777,
  397.         $ino & 0777777,
  398.         $mode & 0777777,
  399.         $uid & 0777777,
  400.         $gid & 0777777,
  401.         $nlink & 0777777,
  402.         $rdev & 0177777,
  403.         $mtime,
  404.         length($nm)+1,
  405.         $size,
  406.         $nm);
  407.     }
  408.     else {
  409.     $cpout{$fh} .= "\0" if length($cpout{$fh}) & 1;
  410.     $cpout{$fh} .= pack("SSSSSSSSLSLa*",
  411.         070707, $dev, $ino, $mode, $uid, $gid, $nlink, $rdev, $mtime,
  412.         length($nm)+1, $size, $nm . (length($nm) & 1 ? "\0" : "\0\0"));
  413.     }
  414.     if ($text ne '') {
  415.     $cpout{$fh} .= $text;
  416.     }
  417.     elsif ($size) {
  418.     &flush($fh) while ($l = length($cpout{$fh})) >= 5120;
  419.     while (sysread(IN, $cpout{$fh}, 5120 - $l, $l)) {
  420.         &flush($fh);
  421.         $l = length($cpout{$fh});
  422.     }
  423.     }
  424.     close IN;
  425. }
  426.  
  427. sub flush {
  428.     local($fh) = @_;
  429.  
  430.     while (length($cpout{$fh}) >= 5120) {
  431.     syswrite($fh,$cpout{$fh},5120);
  432.     ++$blocks{$fh};
  433.     substr($cpout{$fh}, 0, 5120) = '';
  434.     }
  435. }
  436.  
  437. sub flushall {
  438.     $name = 'TRAILER!!!';
  439.     foreach $fh (keys %cpout) {
  440.     &cpio($nc{$fh},$fh);
  441.     $cpout{$fh} .= "0" x (5120 - length($cpout{$fh}));
  442.     &flush($fh);
  443.     print $blocks{$fh} * 10, " blocks\n";
  444.     }
  445. }
  446.  
  447. END
  448. }
  449.  
  450. if ($inittar) {
  451. print <<'START', <<"INTERP", <<'END';
  452. sub tar {
  453.     local($fh) = @_;
  454.     local($linkname,$header,$l,$slop);
  455.     local($linkflag) = "\0";
  456.  
  457. START
  458.     (\$dev,\$ino,\$mode,\$nlink,\$uid,\$gid,\$rdev,\$size,
  459.       \$atime,\$mtime,\$ctime,\$blksize,\$blocks) = $stat\(_\);
  460. INTERP
  461.     $nm = $name;
  462.     if ($nlink > 1) {
  463.     if ($linkname = $linkseen{$fh,$dev,$ino}) {
  464.         $linkflag = 1;
  465.     }
  466.     else {
  467.         $linkseen{$fh,$dev,$ino} = $nm;
  468.     }
  469.     }
  470.     if (-f _) {
  471.     open(IN, "./$_\0") || do {
  472.         warn "Couldn't open $name: $!\n";
  473.         return;
  474.     };
  475.     $size = 0 if $linkflag ne "\0";
  476.     }
  477.     else {
  478.     $linkname = readlink($_);
  479.     $linkflag = 2 if defined $linkname;
  480.     $nm .= '/' if -d _;
  481.     $size = 0;
  482.     }
  483.  
  484.     $header = pack("a100a8a8a8a12a12a8a1a100",
  485.     $nm,
  486.     sprintf("%6o ", $mode & 0777),
  487.     sprintf("%6o ", $uid & 0777777),
  488.     sprintf("%6o ", $gid & 0777777),
  489.     sprintf("%11o ", $size),
  490.     sprintf("%11o ", $mtime),
  491.     "        ",
  492.     $linkflag,
  493.     $linkname);
  494.     $l = length($header) % 512;
  495.     substr($header, 148, 6) = sprintf("%6o", unpack("%16C*", $header));
  496.     substr($header, 154, 1) = "\0";  # blech
  497.     $tarout{$fh} .= $header;
  498.     $tarout{$fh} .= "\0" x (512 - $l) if $l;
  499.     if ($size) {
  500.     &tflush($fh) while ($l = length($tarout{$fh})) >= 10240;
  501.     while (sysread(IN, $tarout{$fh}, 10240 - $l, $l)) {
  502.         $slop = length($tarout{$fh}) % 512;
  503.         $tarout{$fh} .= "\0" x (512 - $slop) if $slop;
  504.         &tflush($fh);
  505.         $l = length($tarout{$fh});
  506.     }
  507.     }
  508.     close IN;
  509. }
  510.  
  511. sub tflush {
  512.     local($fh) = @_;
  513.  
  514.     while (length($tarout{$fh}) >= 10240) {
  515.     syswrite($fh,$tarout{$fh},10240);
  516.     ++$blocks{$fh};
  517.     substr($tarout{$fh}, 0, 10240) = '';
  518.     }
  519. }
  520.  
  521. sub tflushall {
  522.     local($len);
  523.  
  524.     foreach $fh (keys %tarout) {
  525.     $len = 10240 - length($tarout{$fh});
  526.     $len += 10240 if $len < 1024;
  527.     $tarout{$fh} .= "\0" x $len;
  528.     &tflush($fh);
  529.     }
  530. }
  531.  
  532. END
  533. }
  534.  
  535. exit;
  536.  
  537. ############################################################################
  538.  
  539. sub tab {
  540.     local($tabstring);
  541.  
  542.     $tabstring = "\t" x ($indent / 2) . ' ' x ($indent % 2 * 4);
  543.     if (!$statdone) {
  544.     if ($_ =~ /^(name|print|prune|exec|ok|\(|\))/) {
  545.         $delayedstat++;
  546.     }
  547.     else {
  548.         if ($saw_or) {
  549.         $tabstring .= <<"ENDOFSTAT" . $tabstring;
  550. (\$nlink || ((\$dev,\$ino,\$mode,\$nlink,\$uid,\$gid) = $stat\(\$_\))) &&
  551. ENDOFSTAT
  552.         }
  553.         else {
  554.         $tabstring .= <<"ENDOFSTAT" . $tabstring;
  555. ((\$dev,\$ino,\$mode,\$nlink,\$uid,\$gid) = $stat\(\$_\)) &&
  556. ENDOFSTAT
  557.         }
  558.         $statdone = 1;
  559.     }
  560.     }
  561.     $tabstring =~ s/^\s+/ / if $out =~ /!$/;
  562.     $tabstring;
  563. }
  564.  
  565. sub fileglob_to_re {
  566.     local($tmp) = @_;
  567.  
  568.     $tmp =~ s#([./^\$()])#\\$1#g;
  569.     $tmp =~ s/([?*])/.$1/g;
  570.     "^$tmp\$";
  571. }
  572.  
  573. sub n {
  574.     local($n) = @_;
  575.  
  576.     $n =~ s/^-/< / || $n =~ s/^\+/> / || $n =~ s/^/== /;
  577.     $n =~ s/ 0*(\d)/ $1/;
  578.     $n . ')';
  579. }
  580.  
  581. sub quote {
  582.     local($string) = @_;
  583.     $string =~ s/'/\\'/;
  584.     "'$string'";
  585. }
  586.